﻿mainModule.controller("tabularListCtrl", ["$scope", "$rootScope", "itemSvc", "notification", "pageState", "columnSvc", "$routeParams", "articleSvc", "cache", "config", function ($scope, $rootScope, itemSvc, notification, pageState, columnSvc, $routeParams, articleSvc, cache, config) {
    var query;
    $scope.paging = { currentPage: 1, pageSize: config.paging.pageSize, linksSize: config.paging.linksSize };
    $scope.sorting = { column: "ID", ascending: true };
    $scope.showPager = false;    
    $scope.selectedIndex = -1;
        
    var articleID = $routeParams.articleID;    
            
    function buildInitialQuery() {
        var query = { PageNumber: $scope.paging.currentPage, PageSize: $scope.paging.pageSize, ComputePagesCount: true, SortColumn: "ItemID", SortAscending: $scope.sorting.ascending };
        query.ArticleID = articleID;
        return query;
    }

    query = buildInitialQuery();
    findBy(query);    

    function findBy(query) {                                
        itemSvc.dynamicFindBy(query).then(function (response) {
                $scope.columnsList = response.data.ColumnsList;
                $scope.list = response.data.List;
                resetPagination(query, response.data.RowsCount);
            });
    }

    function reloadList(filterQuery) {
        query = angular.extend(query, { PageNumber: 1, ComputePagesCount: true }, filterQuery);
        $scope.list = [];
        findBy(query);
    }    

    function loadListPage(pageNumber) {
        query = angular.extend(query, { PageNumber: pageNumber, ComputePagesCount: false });
        findBy(query);
    }

    function resetPagination(query, itemsCount) {
        $scope.showPager = false;
        if (query.ComputePagesCount) {
            $scope.itemsCount = itemsCount;
        }

        if ((query.PageNumber === 1 && $scope.itemsCount > $scope.paging.pageSize) || (query.PageNumber > 1))
            $scope.showPager = true;

        if ($scope.paging.currentPage != query.PageNumber)
            $scope.paging.currentPage = query.PageNumber;      
    }

    $scope.pageChanged = function () {        
        loadListPage($scope.paging.currentPage)
    }

    $scope.edit = function (index) {
        var item = $scope.list[index];
        $scope.selectedIndex = index;        
        $scope.$broadcast('itemClicked', item.ItemID);
    }

    $scope.remove = function (index) {
        var item = $scope.list[index];
        itemSvc.remove(item.ItemID).then(function (response) {
            if (response.data.Success) {
                $scope.list.splice(index, 1);
                $scope.itemsCount = $scope.itemsCount - 1;
            }

            notification.notify(response.data);            
        });        
    }

    $scope.select = function (index) {
        $scope.selectedIndex = index;
    }

    $scope.add = function() {
        $scope.$broadcast('itemAdding');
    }

    $scope.$on('listReload', function (e, filterQuery) {
        reloadList(filterQuery);
    });

    $scope.$on('itemAdded', function (e, item) {
        var dynamicItem = itemSvc.createDynamicItem(item);
        $scope.list.push(dynamicItem);
        
    });

    $scope.$on('itemEdited', function (e, item) {        
        var dynamicItem = itemSvc.createDynamicItem(item);
        $scope.list[$scope.selectedIndex] = dynamicItem;
    });

    $scope.isFilterVisible = false;
    $scope.toggleFilter = function () {
        $scope.isFilterVisible = !$scope.isFilterVisible;

        if (!$scope.isFilterVisible) {
            query = buildInitialQuery();
            reloadList();
        }
    }

    $scope.activate = function (index) {
        $scope.activeIndex = index;
    }

    $scope.deactivate = function (index) {
        if ($scope.activeIndex == index)
            $scope.activeIndex = -1;
    }
}]);




